/**
 * 
 */
package com.jh.mms.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jh.mms.dao.IHibernateDao;


/**
 * 数据库操作实现类
 * @createDate 2011-7-5 下午05:59:19
 * @since jdk1.6.0_26
 * @version 1.0
 *
 */
public class HiberateDaoImpl extends HibernateDaoSupport implements
		IHibernateDao {

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#delete(java.lang.Class, java.io.Serializable)
	 */
	public void delete(Class clazz, Serializable id) {
		Object obj = this.getHibernateTemplate().get(clazz, id);
		super.getHibernateTemplate().delete(obj);
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#delete(java.lang.Object)
	 */
	public void delete(Object entity) {
		super.getHibernateTemplate().delete(entity);
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#delete(java.util.List)
	 */
	public void delete(List lstObj) {
		super.getHibernateTemplate().deleteAll(lstObj);
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#get(java.lang.Class, java.io.Serializable)
	 */
	public Object get(Class clazz, Serializable id) {
		Object obj = super.getHibernateTemplate().get(clazz, id);
		return obj;
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#getHibernateSession()
	 */
	public Session getHibernateSession() {
		return super.getSession();
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#getNamedQuery(java.lang.String, java.lang.Object[])
	 */
	public List getNamedQuery(String paramString, Object[] params) {
		return super.getHibernateTemplate().findByNamedQuery(paramString, params);
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#getSqlTotal(java.lang.String, java.lang.Object[])
	 */
	public int getSqlTotal(String queryStr, Object[] params) {
		Object obj = sqlQuery(queryStr, params).get(0);
		if(obj == null) {
			obj = 0;
		}
		return Integer.valueOf(obj+"");
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#getTotal(java.lang.String, java.lang.Object[])
	 */
	public int getTotal(String queryStr, Object[] params) {
		Object obj = query(queryStr, params).get(0);
		if(obj == null) {
			obj = 0;
		}
		return Integer.valueOf(obj+"");
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#query(java.lang.String, java.lang.Object[])
	 */
	public List query(String queryStr, Object[] params) {
		return super.getHibernateTemplate().find(queryStr, params);
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#queryPage(java.lang.String, java.lang.String, java.lang.Object[], int, int)
	 */
	public Object[] queryPage(String queryStr, String queryTotalStr,
			Object[] params, int rows, int page) {
		Session session = getHibernateSession();
		Query query = session.createQuery(queryStr);
		if(params != null) {
			for(int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		query.setFirstResult((page - 1) * rows);
		query.setMaxResults(rows);
		List list = query.list();
		int totalRows = getTotal(queryTotalStr, params);
		int totalPage = 0; 
		if(totalRows % rows == 0) {
			totalPage = totalRows / rows;
		}else {
			totalPage = totalRows / rows + 1;
		}
		return new Object[]{list, totalRows, totalPage};
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#queryPage(java.lang.String, int, java.lang.Object[], int, int)
	 */
	public Object[] queryPage(String queryStr, int total, Object[] params,
			int rows, int page) {
		Session session = getHibernateSession();
		Query query = session.createQuery(queryStr);
		if(params != null) {
			for(int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		query.setFirstResult((page - 1) * rows);
		query.setMaxResults(rows);
		List list = query.list();
		int totalPage = 0; 
		if(total % rows == 0) {
			totalPage = total / rows;
		}else {
			totalPage = total / rows + 1;
		}
		return new Object[]{list, total, totalPage};
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#queryPageData(java.lang.String, java.lang.Object[], int, int)
	 */
	public List queryPageData(String queryStr, Object[] params, int rows,
			int page) {
		Session session = getHibernateSession();
		Query query = session.createQuery(queryStr);
		if(params != null) {
			for(int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		query.setFirstResult((page - 1) * rows);
		query.setMaxResults(rows);
		return query.list();
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#save(java.lang.Object)
	 */
	public void save(Object entity) {
		super.getHibernateTemplate().save(entity);
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#save(java.util.List)
	 */
	public void save(List lstObj) {
		super.getHibernateTemplate().saveOrUpdateAll(lstObj);
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#saveOrUpdate(java.lang.Object)
	 */
	public void saveOrUpdate(Object entity) {
		super.getHibernateTemplate().saveOrUpdate(entity);
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#saveOrUpdate(java.util.List)
	 */
	public void saveOrUpdate(List lstObj) {
		super.getHibernateTemplate().saveOrUpdateAll(lstObj);
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#sqlQuery(java.lang.String, java.lang.Object[])
	 */
	public List sqlQuery(String queryStr, Object[] params) {
		Session session = getHibernateSession();
		Query query = session.createSQLQuery(queryStr);
		if(params != null) {
			for(int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		return query.list();
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#updateOrDelete(java.lang.String, java.lang.Object[])
	 */
	public void updateOrDelete(String paramString, Object[] params) {
		Session session = getHibernateSession();
		Query query = session.createQuery(paramString);
		if(params != null) {
			for(int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		query.executeUpdate();
	}

	/* (non-Javadoc)
	 * @see com.chinaboxun.platform.core.dao.IHibernateDao#useSqlUpdateOrDelete(java.lang.String, java.lang.Object[])
	 */
	public void useSqlUpdateOrDelete(String paramString, Object[] params) {
		Session session = getHibernateSession();
		Query query = session.createSQLQuery(paramString);
		if(params != null) {
			for(int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		query.executeUpdate();
	}

	public Object[] sqlQueryPage(String queryStr, String queryTotalStr,
			Object[] params, int rows, int page) {
		Session session = getHibernateSession();
		Query query = session.createSQLQuery(queryStr);
		if(params != null) {
			for(int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		query.setFirstResult((page - 1) * rows);
		query.setMaxResults(rows);
		List list = query.list();
		int totalRows = getTotal(queryTotalStr, params);
		int totalPage = 0; 
		if(totalRows % rows == 0) {
			totalPage = totalRows / rows;
		}else {
			totalPage = totalRows / rows + 1;
		}
		return new Object[]{list, totalRows, totalPage};
	}

	public Object[] sqlQueryPage(String queryStr, int total, Object[] params,
			int rows, int page) {
		Session session = getHibernateSession();
		Query query = session.createSQLQuery(queryStr);
		if(params != null) {
			for(int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		query.setFirstResult((page - 1) * rows);
		query.setMaxResults(rows);
		List list = query.list();
		int totalPage = 0; 
		if(total % rows == 0) {
			totalPage = total / rows;
		}else {
			totalPage = total / rows + 1;
		}
		return new Object[]{list, total, totalPage};
	}

	public List sqlQueryPageData(String queryStr, Object[] params, int rows,
			int page) {
		Session session = getHibernateSession();
		Query query = session.createSQLQuery(queryStr);
		if(params != null) {
			for(int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
		query.setFirstResult((page - 1) * rows);
		query.setMaxResults(rows);
		return query.list();
	}

}
